package com.ruoyi.business.service.impl;

import java.util.List;

import cn.hutool.core.lang.UUID;
import com.ruoyi.business.mapper.MyOrderFromDao;
import com.ruoyi.business.service.ITSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TOrderFromMapper;
import com.ruoyi.business.domain.TOrderFrom;
import com.ruoyi.business.service.ITOrderFromService;

import javax.annotation.Resource;

/**
 * 订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-13
 */
@Service
public class TOrderFromServiceImpl implements ITOrderFromService 
{
    @Autowired
    private TOrderFromMapper tOrderFromMapper;

    /**
     * 查询订单管理
     * 
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public TOrderFrom selectTOrderFromById(Long id)
    {
        return tOrderFromMapper.selectTOrderFromById(id);
    }

    /**
     * 查询订单管理列表
     * 
     * @param tOrderFrom 订单管理
     * @return 订单管理
     */
    @Override
    public List<TOrderFrom> selectTOrderFromList(TOrderFrom tOrderFrom)
    {
        return tOrderFromMapper.selectTOrderFromList(tOrderFrom);
    }

    /**
     * 新增订单管理
     * 
     * @param tOrderFrom 订单管理
     * @return 结果
     */
    @Override
    public int insertTOrderFrom(TOrderFrom tOrderFrom)
    {
        return tOrderFromMapper.insertTOrderFrom(tOrderFrom);
    }

    /**
     * 修改订单管理
     * 
     * @param tOrderFrom 订单管理
     * @return 结果
     */
    @Override
    public int updateTOrderFrom(TOrderFrom tOrderFrom)
    {
        return tOrderFromMapper.updateTOrderFrom(tOrderFrom);
    }

    /**
     * 批量删除订单管理
     * 
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTOrderFromByIds(Long[] ids)
    {
        return tOrderFromMapper.deleteTOrderFromByIds(ids);
    }

    /**
     * 删除订单管理信息
     * 
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteTOrderFromById(Long id)
    {
        return tOrderFromMapper.deleteTOrderFromById(id);
    }

    ////////////////////////////////////////////////////////////////////         用户下单逻辑
    @Resource
    private MyOrderFromDao myOrderFromDao;
    @Override
    public void createOrder(TOrderFrom tOrderFrom) {
        // 获取总单
        TOrderFrom userOrder = getUserOrder(tOrderFrom);
        // 添加sku未付款订单( 购物车单 )
        tOrderFrom.setpId(userOrder.getId());
        insertTOrderFrom(tOrderFrom);
    }

    /// 获取用户总单
    private  TOrderFrom getUserOrder(TOrderFrom tOrderFrom) {
        // 查询
        TOrderFrom select;
        select = new TOrderFrom();
        select.setUserId(tOrderFrom.getUserId());
        select.setpId(0L);

        List<TOrderFrom> tOrderFroms = selectTOrderFromList(select);
        if(tOrderFroms.isEmpty()) {
            select.setGrade(1L);
            select.setType(0L);
            select.setUserId(tOrderFrom.getUserId());
            select.setUserName(tOrderFrom.getUserName());
            String uuid = UUID.randomUUID().toString().replace("-", "");
            select.setNo("root:"+uuid);
            // 新整
            tOrderFromMapper.insertTOrderFrom(select);
            return select;
        }
        return tOrderFroms.get(0);
    }

    // 获取未付款订单
    private  TOrderFrom getUserUnPayOrder(TOrderFrom tOrderFrom) {
        // 查询
        TOrderFrom select;
        select = new TOrderFrom();
        select.setUserId(tOrderFrom.getUserId());
        select.setpId(0L);
        select.setStatus(0L);
        List<TOrderFrom> tOrderFroms =  selectTOrderFromList(select);
        if(tOrderFroms.isEmpty()) {
            return null;
        }
        return tOrderFroms.get(0);
    }
}
